﻿namespace MetaProperties.Core
{
    using System.Collections.Generic;

    using MetaProperties.Framework;

#if ANCESTRY_SUPPORTED
    using MetaProperties.Framework.Ancestry;
#endif

    /// <summary>
    /// An observable value.
    /// </summary>
    /// <typeparam name="T">The type of the contained value.</typeparam>
    public class Observable<T> : TypedObservable<T>, IObservable<T>
    {
        /// <summary>
        /// The contained value.
        /// </summary>
        private T value;

        /// <summary>
        /// Initializes a new instance of the <see cref="Observable&lt;T&gt;"/> class.
        /// </summary>
        public Observable()
            : this(null)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Observable&lt;T&gt;"/> class.
        /// </summary>
        /// <param name="defaultValue">The default value.</param>
        public Observable(T defaultValue)
            : this(null, defaultValue)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Observable&lt;T&gt;"/> class.
        /// </summary>
        /// <param name="comparer">The comparer.</param>
        public Observable(IComparer<T> comparer)
            : this(comparer, default(T))
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Observable&lt;T&gt;"/> class.
        /// </summary>
        /// <param name="comparer">The comparer.</param>
        /// <param name="defaultValue">The default value.</param>
        public Observable(IComparer<T> comparer, T defaultValue)
            : base(comparer)
        {
            this.value = defaultValue;
#if ANCESTRY_SUPPORTED
            this.UpdateValueParent(this.value, UpdateParentOperation.Add);
#endif
        }

        /// <summary>
        /// Gets the value.
        /// </summary>
        /// <returns>The contained value.</returns>
        protected override object GetValue()
        {
            return this.Value;
        }

        /// <summary>
        /// Gets or sets the value of the observable property.
        /// </summary>
        /// <value>The contained value.</value>
        public T Value
        {
            get
            {
                return this.value;
            }

            set
            {
                if (!this.AreEqual(this.value, value))
                {
                    this.OnValueChanging();

#if ANCESTRY_SUPPORTED
                    this.UpdateValueParent(this.value, UpdateParentOperation.Remove);
#endif

                    T oldValue = this.value;
                    this.value = value;

#if ANCESTRY_SUPPORTED
                    this.UpdateValueParent(this.value, UpdateParentOperation.Add);
#endif

                    this.OnValueChanged(
                        oldValue,
                        value);
                }
                else
                {
                    this.OnValueSet(
                        value);
                }
            }
        }
    }
}
